package com.uphone.magicleafteacher.pro.activity.listenertrainscore;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.radish.baselibrary.Intent.IntentData;
import com.radish.baselibrary.Intent.IntentUtils;
import com.radish.baselibrary.utils.ActivityCollector;
import com.radish.baselibrary.utils.LogUtils;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.mvp.BaseMvpActivity;
import com.uphone.magicleafteacher.http.RequestBodyUtil;
import com.uphone.magicleafteacher.pro.activity.MytrainingdetailsActivity;
import com.uphone.magicleafteacher.pro.activity.listenertraining.ListenerTrainingActivity;
import com.uphone.magicleafteacher.util.AudioPlayProgressUtil;
import com.uphone.magicleafteacher.util.CheckButton;
import com.uphone.magicleafteacher.util.DateUtils;
import com.uphone.magicleafteacher.view.RecordLoadingView1;
import com.uphone.magicleafteacher.vo.ListenerTrainEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;

/**
 * 评分页面
 * Time: 2019/10/29
 */
public class ListenerTrainScoreActivity extends BaseMvpActivity<ListenerTrainScorePresenter> implements ListenerTrainScoreContract.View {

    @IntentData
    private String id;
    @IntentData
    private String title = "";
    @BindView(R.id.et_score1)
    EditText etScore1;
    @BindView(R.id.et_score2)
    EditText etScore2;
    @BindView(R.id.et_score3)
    EditText etScore3;
    @BindView(R.id.et_score4)
    EditText etScore4;
    @BindView(R.id.et_score5)
    EditText etScore5;
    @BindView(R.id.et_score6)
    TextView etScore6;
    @BindView(R.id.et_content)
    TextView etContent;
    @BindView(R.id.btn_submit)
    CheckButton btnSubmit;
    @BindView(R.id.recordLoadingView)
    RecordLoadingView1 recordLoadingView;
    @BindView(R.id.tv_duration)
    TextView tvDuration;
    private File videoFile;


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_train_score;
    }

    @Override
    protected void initView() {
        btnSubmit.setRelaViews(new CheckButton.ChangeCallback() {
            @Override
            public void click(boolean enable) {
                if (enable) {
                    if (videoFile == null || !videoFile.exists()) {
                        ToastUtils.showShort("请录制指导音频");
                        return;
                    }
                    Map<String, RequestBody> params = new HashMap<>();
                    params.put("workId", RequestBodyUtil.convertToRequestBody(id + ""));
                    params.put("remark", RequestBodyUtil.convertToRequestBody(etContent.getText().toString()));
                    params.put("coordinateScore", RequestBodyUtil.convertToRequestBody(etScore1.getText().toString()));
                    params.put("accuracyScore", RequestBodyUtil.convertToRequestBody(etScore2.getText().toString()));
                    params.put("imitationScore", RequestBodyUtil.convertToRequestBody(etScore3.getText().toString()));
                    params.put("fluencyScore", RequestBodyUtil.convertToRequestBody(etScore4.getText().toString()));
                    params.put("thoughtScore", RequestBodyUtil.convertToRequestBody(etScore5.getText().toString()));
                    params.put("jifen", RequestBodyUtil.convertToRequestBody(etScore6.getText().toString()));
                    mPresenter.giveAMark(params, videoFile);
                } else {
                    ToastUtils.showShort("请完善评分");
                }
            }
        }, etScore1, etScore2, etScore3, etScore4, etScore5, etScore6, etContent);
        TextChang textChang = new TextChang();
        etScore1.addTextChangedListener(textChang);
        etScore2.addTextChangedListener(textChang);
        etScore3.addTextChangedListener(textChang);
        etScore4.addTextChangedListener(textChang);
        etScore5.addTextChangedListener(textChang);

    }

    @Override
    public void giveAMark() {
        IntentUtils.getInstance().with(ListenerTrainScoreActivity.this, MytrainingdetailsActivity.class)
                .putString("title", title)
                .putString("id", id)
                .start();
        EventBus.getDefault().post(new ListenerTrainEvent());
        ActivityCollector.removeActivity(ListenerTrainingActivity.class, ListenerTrainScoreActivity.class);
    }

    class TextChang implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            try {
                String score1 = etScore1.getText().toString();
                String score2 = etScore2.getText().toString();
                String score3 = etScore3.getText().toString();
                String score4 = etScore4.getText().toString();
                String score5 = etScore5.getText().toString();
                int scor1 = Integer.valueOf(score1).intValue();
                int scor2 = Integer.valueOf(score2).intValue();
                int scor3 = Integer.valueOf(score3).intValue();
                int scor4 = Integer.valueOf(score4).intValue();
                int scor5 = Integer.valueOf(score5).intValue();
                int sum = scor1 + scor2 + scor3 + scor4 + scor5;
                etScore6.setText("" + sum + "");
            } catch (Exception ignored) {

            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    }


    @Override
    protected void initTitle() {
        initTitle("评分");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.recordLoadingView})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.recordLoadingView:
                if (recordLoadingView.getType() != 1) {
                    // 录制
                    tvDuration.setVisibility(View.GONE);
                    recordLoadingView.setPlaying(this, 1);
                } else {
                    recordLoadingView.setPlaying(this, 2, result -> {
                        LogUtils.e("result:" + result.getAbsolutePath());
                        if (result.exists()) {//录音完成
                            ToastUtils.showShort("录音完成");
                            videoFile = result;
                            AudioPlayProgressUtil.getDurationByUrl(videoFile.getAbsolutePath(), duration -> {
                                tvDuration.setText(DateUtils.getTimeFormLong(duration) + "");
                                tvDuration.setVisibility(View.VISIBLE);
                            });
                        } else {
                            ToastUtils.showShort("录音失败");
                            videoFile = null;
                            recordLoadingView.setPlaying(this, 0);
                        }
                    });
                }

                break;
        }
    }
}
